﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/jquery-1.10.2.min.js"></script>
</head>
<body>
    <button id="getauthorurl">1、点击授权url地址</button><br />
    <p>
        <a id="baseurl">2-1、开始基本授权</a>
        基本授权url：<span id="baseurlmsg" style="color:red"></span>
    </p>
    <p>
        <a id="allurl">2-2、开始完全授权</a>
        完全授权url：<span id="allurlmsg" style="color:red"></span>
    </p>
    <p>授权信息state：<span id="statemsg" style="color:red"></span></p>
    <hr />

    <p>
        <button id="userinfocallback">3、获取用户信息</button>
    </p>
    <button id="pay">4、点击支付</button>

    <script>
        $(function () {
            var openid;
            //点击获取授权
            $('#getauthorurl').click(function () {
                $.get('api/wechat/getauthorurl',
                    function (data) {
                        console.log(data);
                        $('#baseurlmsg').html(data.BaseAuthorizeUrl);
                        $('#allurlmsg').html(data.UserinfoAuthorizeUrl);
                        $('#statemsg').html(data.State);
                        $('#baseurl').attr('href', data.BaseAuthorizeUrl);
                        $('#allurl').attr('href', data.UserinfoAuthorizeUrl);
                    });
            });

            //获取用户信息
            $('#userinfocallback').click(function () {
                var query = window.location.search;
                $.get('api/wechat/userinfocallback' + query,
                    function (data) {
                        console.log(data);
                        openid = data.openid;
                    });
            });

            //支付
            $('#pay').click(function () {
                $.get('api/wechat/payinfo?orderId=a072477e50be4db3bee4359beed65b31&openId=' + openid,
                    function (data) {
                        console.log(data);
                        wxpay(data.AppId, data.TimeStamp, data.NonceStr, data.Package, data.PaySign);
                    });
            });
        });

        function wxpay(appid, timeStamp, nonceStr, packagestr, paySign) {
            WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": appid, //公众号名称，由商户传入
                "timeStamp": timeStamp, //时间戳
                "nonceStr": nonceStr, //随机串
                "package": packagestr,//扩展包
                "signType": "MD5", //微信签名方式:MD5
                "paySign": paySign //微信签名
            }, function (res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    alert('pay ok');
                }
            });
        }
    </script>
</body>
</html>
